var successUpdateCallback = function (data) {
    //清空原来数据
    $("#hotelBody").html('');
    var hotels = data.data;
    for (var i = 0; i < hotels.length; i++) {
        var html = '<tr>';
        html += '<td><input type="checkbox" value="' + hotels[i].id + '" /></td>';
        html += '<td>' + hotels[i].username + '</td>';
        html += '<td>' + hotels[i].name + '</td>';
        html += '<td>' + hotels[i].cityname + '</td>';
        html += '<td>' + hotels[i].areaname + '</td>';
        html += '<td>' + hotels[i].enddate + '</td>';
        html += '</tr>';
        $("#hotelBody").append(html);
    }
};
var errorUpdateCallback = function (data) {
    if (typeof (data.responseJSON) == "undefined") {
        tooltipWrong($('#requestConfuse'), '系统内部错误', 3);
    } else {
        tip = data.responseJSON.info;
        tooltipWrong($('#requestConfuse'), tip, 3);
    }
};

$(function () {
    $('.detail').click(function () {
        var params = $(this).attr('data-json');
        params = JSON.parse(params); 
        $('#name').text(params.name);
        $('#areacode').text(params.areacode);
        $('#mobile').text(params.mobile);
        $('#idcard').text(params.idcard);
        $('#idcardpath').attr('src',params.idcardpath);
    });
    $('.pass').click(function () {
        $('#requestPass').data('apply', $(this).attr('data-json'));
    });
    $('.confuse').click(function () {
        $('#requestConfuse').data('applyId', $(this).attr('data-value'));
    });
    $('#requestEnddate').datepicker({
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        autoclose: true,
    });
    $('#requestPass').click(function () {
        var enddate = $('#requestEnddate').val();
        if (!enddate) {
            $('#requestEnddate').focus();
            return false;
        }
        var params = $(this).data('apply');
        params = JSON.parse(params); 
        params.enddate = enddate; 
        ajaxRequest('/Home/Dealer/handle', params, 'post', function (data) {
            $('#passModel').modal('hide');
            location.reload();
        }, null);
    });
    $('#requestConfuse').click(function () {
        var submitBtn = $(this);
        var params = new Object();
        params.id = $(this).data('applyId');
        ajaxRequest('/Home/Dealer/setStatus', params, 'post', function (data) {
            $('#confuseModel').modal('hide');
            location.reload();
        }, null);
    });
    $(".view").on('click', function (e) {
        e.preventDefault();
        var params = new Object();
        params.dealerId = $(this).attr("data-value");
        ajaxRequest('/Home/Dealer/getHotel', params, 'post', successUpdateCallback, errorUpdateCallback);
    });
    $('.enddate').dblclick(function(){
        //$(this).addClass('hidden');
        var enddate = $(this);
        var value = enddate.text();
        var dealerId = enddate.attr('data-value');
        $(this).data('date',value);
        $(this).data('dealerId',dealerId);
        $(this).html("<input type='text' id='requestEditEndtime' maxlength='10' readonly='readonly' />");   
        $(this).children().val(value);
        $('#requestEditEndtime').datepicker({
            format: 'yyyy-mm-dd',
            language: "zh-CN",
            autoclose: true,
        }).on('changeDate',function(ev){
            var value = $(this).val();
            var params = {};
            params.enddate = value;
            params.dealerId = dealerId;
            $.post('/Home/Dealer/setEndtime', params, function(data){
                var info = data.data;
                enddate.html(info.enddate);
            });
        }).on('hide', function(event) {
            var value = $(this).val();
            enddate.html(value);    
        });
        $(this).children().focus();
    });
});
